# !/usr/bin/env python3
# -*- coding: utf-8 -*-

from lib.tsn_builder_lib import host_to_net
import xml.etree.ElementTree as EleTree
from tsn_builder_struct.para_tbl_structure import ParaTbl, ParaTblEachNode


def link_para_tbl(node,
                  para_tbl,
                  reg_addr):
    # 找到该节点对应的参数表
    node_id = node['src_node']
    tbl_each_node = []
    node_type = ''
    for tbl in para_tbl.tbl_set:
        if tbl.node_id == node_id:
            tbl_each_node = tbl.tbl_each_node
            node_type = tbl.node_type
            break

    if not tbl_each_node:
        return []

    dic = []

    addr1 = reg_addr.rc_threshold['addr']
    addr1_str = host_to_net(addr1)
    value1 = tbl_each_node.byte.entry1
    value1_str = host_to_net(value1)
    addr_value = {'addr': addr1_str,
                  'value': [value1_str]}
    dic.append(addr_value)

    addr2 = reg_addr.hpriority_policing_threshold['addr']
    addr2_str = host_to_net(addr2)
    value2 = tbl_each_node.byte.entry2
    value2_str = host_to_net(value2)
    addr_value = {'addr': addr2_str,
                  'value': [value2_str]}
    dic.append(addr_value)

    addr3 = reg_addr.lpriority_policing_threshold['addr']
    addr3_str = host_to_net(addr3)
    value3 = tbl_each_node.byte.entry3
    value3_str = host_to_net(value3)
    addr_value = {'addr': addr3_str,
                  'value': [value3_str]}
    dic.append(addr_value)

    addr4 = reg_addr.qbv_or_qch['addr']
    addr4_str = host_to_net(addr4)
    value4 = tbl_each_node.byte.entry4
    value4_str = host_to_net(value4)
    addr_value = {'addr': addr4_str,
                  'value': [value4_str]}
    dic.append(addr_value)

    if node_type == 'SW': # SW代表交换机，NIC代表网卡
        addr5 = reg_addr.token_bucket_size['addr']
        addr5_str = host_to_net(addr5)
        value5 = tbl_each_node.byte.entry5
        value5_str = host_to_net(value5)
        addr_value = {'addr': addr5_str,
                      'value': [value5_str]}
        dic.append(addr_value)

        addr6 = reg_addr.add_tokens_num_perround['addr']
        addr6_str = host_to_net(addr6)
        value6 = tbl_each_node.byte.entry6
        value6_str = host_to_net(value6)
        addr_value = {'addr': addr6_str,
                      'value': [value6_str]}
        dic.append(addr_value)

    addr8 = reg_addr.schedule_period['addr']
    addr8_str = host_to_net(addr8)
    value8 = tbl_each_node.byte.entry8
    value8_str = host_to_net(value8)
    addr_value = {'addr': addr8_str,
                  'value': [value8_str]}
    dic.append(addr_value)

    addr9 = reg_addr.cycle_start_cyclen['addr']
    addr9_str = host_to_net(addr9)
    value9 = tbl_each_node.byte.entry9
    value9_str = host_to_net(value9)
    addr_value = {'addr': addr9_str,
                  'value': [value9_str]}
    dic.append(addr_value)

    addr10 = reg_addr.time_slot_len['addr']
    addr10_str = host_to_net(addr10)
    value10 = tbl_each_node.byte.entry10
    value10_str = host_to_net(value10)
    addr_value = {'addr': addr10_str,
                  'value': [value10_str]}
    dic.append(addr_value)

    return dic


def _get_para_tbl(para_tbl_xml):
    para_set = []

    root = EleTree.parse(para_tbl_xml)
    node_ele_set = root.findall('node')
    for node_ele in node_ele_set:
        para_dic = {}
        node_ele_list = list(node_ele)
        for para in node_ele_list:
            text = int(para.text)
            para_dic.update({para.tag: text})
        if para_dic['node_type'] == 0: # 交换机
            para_dic.update({'node_type': 'SW'})
        elif para_dic['node_type'] == 1: # 网卡
            para_dic.update({'node_type': 'NIC'})
        para_set.append(para_dic)
    return para_set


def _init_para_tbl(para_set):
    para_tbl = ParaTbl()
    for entry_each_node in para_set:
        # print(entry_each_node)
        para_tbl_each_node = ParaTblEachNode(**entry_each_node)
        para_tbl.append_tbl(para_tbl_each_node)

    return para_tbl


def read_para_tbl(para_tbl_xml):
    para_set = _get_para_tbl(para_tbl_xml)
    # [print(entry) for entry in para_set]
    para_tbl = _init_para_tbl(para_set)
    return para_tbl


def main():
    para_tbl_xml = '../output/tbl/para_tbl.xml'
    read_para_tbl(para_tbl_xml)


if __name__ == '__main__':
    main()
